﻿using System;
using System.Collections.Generic;
using ZX.Web.UI.Controls;
using ZX.BLL;
using ZX.DBModule;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.CRM.Bussiness;
using ZX.Common;
using ZX.CRM.Library;
using ZX.Web.UI.Controls.ControlBind;
using System.Data;
using ZX.CRM.Web.CrmWorkFlowService;

namespace ZX.WebApp.Crm.Deliver
{
    public partial class AddDeliverMajorProject : EditUIPage
    {
        BTCrmCrustomerOrder order = new BTCrmCrustomerOrder();
        BTCrmCustomer bll = new BTCrmCustomer();
        BDatPhraseDetail bllDetail = new BDatPhraseDetail();
        BCrmDeliverMajor bllDeliverMajorProject = new BCrmDeliverMajor();
        string strDeliverMajorInfo = "[]", unSatisy = "", wfid = "",orderProduct="", wtid = "", OperStatus = "",orderId="",productId="";
        WorkFlowServiceClient client = new WorkFlowServiceClient();
        /// <summary>
        /// 退回标志
        /// </summary>
        protected string strBack { get; set; }
        public string workflowInsId
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["WorkflowInsId"] != null)
            {
                workflowInsId = Request["WorkflowInsId"].ToString();
                Inits(workflowInsId);
            }
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string ids = this.GetStringFromQueryString("id", "");
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(ids) as BaseEntity;
            this.EditTemplate.Text = CreateInstanceJsObject(instance, entity);
            //交付单状态
            IList<DatPhraseDetail> ListDliverId = bllDetail.GetDatPhraseDetailByClassValue("DeliverState");
            DropDownListControl.DropDataSourceBind(this.DliverState, ListDliverId, "PhraseText", "PhraseID");
            if (Flag == "0")
            {
                this.Code.Text = IDMaker.GetSequenceStr("DELP001");
                this.AddTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                this.ID.Value = Guid.NewGuid().ToString();
                DliverState.SelectedValue = "8A41F2EF-989F-4871-BBE9-91CDFF4235CA";
                this.txtXm.Value = this.User.UName;
                this.WorkFlowInsId.Value = Guid.NewGuid().ToString();
                this.WorkTaskInsId.Value = Guid.NewGuid().ToString();
            }
            if (Flag == "1")
            {
                string strId = this.GetStringFromQueryString("id", "");
                if (strId == "")
                {
                    strId = this.GetStringFromQueryString("WorkflowInsId", "");
                }
                IList<V_CrmDeliverMajorProInfo> instanceMajor = bllDeliverMajorProject.GetAllDeliverMajorProInfo(new Guid(strId));
                if (instanceMajor != null && instanceMajor.Count > 0)
                {
                    btnOrderProductID.Visible = false;

                    btnCustomerMgr.Visible = false;
                    btnCustomerDirector.Visible = false;
                    btnCustomerManager.Visible = false;

                    btnClassFrontServer.Visible = false;
                    if (strDescript == "jajd")
                    {
                        DliverState.SelectedValue = "20F825C4-292A-4386-8157-923668BD2682";
                        btnMemember.Visible = false;
                        btnPM.Visible = false;
                        txtStartDate.Disabled = true;
                        txtPmExplain.Disabled = true;
                    }
                    else if (strDescript == "jgdc")
                    {
                        btnMemember.Visible = false;
                        btnPM.Visible = false;
                        txtStartDate.Disabled = true;
                        txtPmExplain.Disabled = true;
                        txtDay.Disabled = true;
                        txtLeaveDate.Disabled = true;
                        txtPause.Disabled = true;
                        txtEndTime.Disabled = true;
                        txtReStart.Disabled = true;
                        txtExplain.Disabled = true;
                    }
                    else if (strDescript == "hxfw")
                    {
                        btnMemember.Visible = false;
                        btnPM.Visible = false;
                        txtStartDate.Disabled = true;
                        txtPmExplain.Disabled = true;
                        txtDay.Disabled = true;
                        txtLeaveDate.Disabled = true;
                        txtPause.Disabled = true;
                        txtReStart.Disabled = true;
                        ddlSatisfy.Enabled = false;
                        txtExplain.Disabled = true;
                        txtEndTime.Disabled = true;
                        txtMarketExplain.Disabled = true;
                    }
                    else
                    {
                        if (strDescript != "xmqd") { btnClassFrontServer.Visible = true; }
                    }
                    orderId = instanceMajor[0].OrderID.ToString();
                    productId = instanceMajor[0].ProductId.ToString();
                    orderProduct = instanceMajor[0].OrderProductID.ToString();
                    this.ID.Value = instanceMajor[0].ID.ToString();
                    this.Code.Text = instanceMajor[0].Code;
                    this.AddTime.Text = instanceMajor[0].AddTime.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].AddTime.ToString("yyyy-MM-dd");
                    this.txtOrderProductID.Value = instanceMajor[0].OrderCode;
                    this.CustomerCode.Value = instanceMajor[0].CustomerCode;
                    this.CustomerName.Value = instanceMajor[0].CustomerName;
                    this.Pay1.Value = instanceMajor[0].AccountReceivable.ToString();
                    this.Pay2.Value = instanceMajor[0].ResignMoney.ToString();
                    this.txtClassFrontServer.Value = instanceMajor[0].FzjfName;

                    this.CustomerMgr.Value = instanceMajor[0].CustomerMgr.ToString();
                    this.txtCustomerMgr.Value = instanceMajor[0].CustomerMgrName;
                    this.CustomerDirector.Value = instanceMajor[0].CustomerDirector.ToString();
                    this.txtCustomerDirector.Value = instanceMajor[0].CustomerDirectorName.ToString();
                    this.CustomerManager.Value = instanceMajor[0].CustomerManager.ToString();
                    this.txtCustomerManager.Value = instanceMajor[0].CustomerManagerName.ToString();

                    this.txtXm.Value = instanceMajor[0].UName;
                    this.txtStartDate.Value = instanceMajor[0].BeginDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].BeginDate.ToString("yyyy-MM-dd");
                    this.txtPmExplain.Value = instanceMajor[0].PExplain;
                    this.txtPM.Value = instanceMajor[0].PMName;//项目经理
                    this.txtAddTime.Value = instanceMajor[0].ProjectDate.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].ProjectDate.ToString("yyyy-MM-dd"); //实施填写时间
                    if ((instanceMajor[0].ProjectUser.ToString().Length > 0 && instanceMajor[0].ProjectUser != Guid.Empty) || strView == "check")
                    {
                        this.AddUser.Value = instanceMajor[0].ProjectUser.ToString(); //项目填写人ID
                        this.txtAddUser.Value = instanceMajor[0].AddPmName;//项目填写人
                    }
                    else
                    {
                        this.AddUser.Value = this.UserId.ToString();
                        this.txtAddUser.Value = this.User.UName;
                    }
                    this.txtDay.Value = instanceMajor[0].DateSum.ToString() == "0" ? "" : instanceMajor[0].DateSum.ToString();
                    this.txtMemember.Value = instanceMajor[0].StudentName;//项目成员
                    this.txtEndTime.Value = instanceMajor[0].EndDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].EndDate.ToString("yyyy-MM-dd");
                    this.txtExplain.Value = instanceMajor[0].SsExplain;
                    this.txtEndWriteTime.Value = instanceMajor[0].SsDate.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].SsDate.ToString("yyyy-MM-dd"); //实施填写时间
                    if ((instanceMajor[0].SsUser.ToString().Length > 0 && instanceMajor[0].SsUser != Guid.Empty)||strView=="check")
                    {
                        this.hdnEnd.Value = instanceMajor[0].SsUser.ToString();
                        this.txtEndUser.Value = instanceMajor[0].SsUName;
                    }
                    else
                    {
                        this.hdnEnd.Value = this.UserId.ToString();
                        this.txtEndUser.Value = this.User.UName;
                    }
                    //离项日期
                    this.txtLeaveDate.Value = (instanceMajor[0].LeaveProjectDate.ToString("yyyy-MM-dd") == "0001-01-01" || instanceMajor[0].LeaveProjectDate.ToString("yyyy-MM-dd") == "1900-01-01") ? "" : instanceMajor[0].LeaveProjectDate.ToString("yyyy-MM-dd"); //实施填写时间
                    //项目暂停日期
                    this.txtPause.Value = (instanceMajor[0].ProejectPauseDate.ToString("yyyy-MM-dd") == "0001-01-01" || instanceMajor[0].ProejectPauseDate.ToString("yyyy-MM-dd") == "1900-01-01") ? "" : instanceMajor[0].ProejectPauseDate.ToString("yyyy-MM-dd"); //实施填写时间
                    //项目重启日期
                    this.txtReStart.Value = (instanceMajor[0].ProjectRestartDate.ToString("yyyy-MM-dd") == "0001-01-01" ||  instanceMajor[0].ProjectRestartDate.ToString("yyyy-MM-dd") =="1900-01-01") ? "" : instanceMajor[0].ProjectRestartDate.ToString("yyyy-MM-dd"); //实施填写时间
                    //退款
                    unSatisy = instanceMajor[0].UnSatisfy.ToString();
                    //后续服务
                    txtServiceName.Value = instanceMajor[0].ServiceName;
                    ServiceDate.Value = instanceMajor[0].ServiceDate.ToString("yyyy-MM-dd") == "0001-01-01" ? "" : instanceMajor[0].ServiceDate.ToString("yyyy-MM-dd"); //后续服务填写时间
                    ddlService.SelectedValue = instanceMajor[0].ServiceSatisfy.ToString() != "0" ? "1" : "0";
                    ddlAdvisor.SelectedValue = instanceMajor[0].AdvisorIsAt.ToString() != "0" ? "1" : "0";
                    ddlSuccess.SelectedValue = instanceMajor[0].SucceedDegree.ToString();
                    //this.txtSucceed.Value = instanceMajor[0].SucceedDegree.ToString() == "0" ? "" : instanceMajor[0].SucceedDegree.ToString();
                    this.ddlSatisfy.SelectedValue = instanceMajor[0].WellDegree.ToString() != "0" ? "1" : "0";
                    this.txtMydTime.Value = instanceMajor[0].DegreeDate.ToString("yyyy-MM-dd") == "0001-01-01" ? strView == "check" ? "" : DateTime.Now.ToString("yyyy-MM-dd") : instanceMajor[0].DegreeDate.ToString("yyyy-MM-dd"); //市场填写时间
                    if ((instanceMajor[0].DegreeUser.ToString().Length > 0 && instanceMajor[0].DegreeUser != Guid.Empty) || strView == "check")
                    {
                        this.hdnMyd.Value = instanceMajor[0].DegreeUser.ToString();
                        this.txtMydUser.Value = instanceMajor[0].MarketName;
                    }
                    else
                    {
                        this.hdnMyd.Value = this.UserId.ToString();
                        this.txtMydUser.Value = this.User.UName;
                    }
                    this.txtMarketExplain.Value = instanceMajor[0].Explain;

                    this.Memember.Value = instanceMajor[0].PMemember;//项目成员ID
                    this.PM.Value = instanceMajor[0].PM.ToString();
                    IList<BaseEntity> _listDeliverMajorInfo = new List<BaseEntity>();
                    foreach (var _e in instanceMajor)
                    {
                        _listDeliverMajorInfo.Add(_e);
                    }
                    strDeliverMajorInfo = _listDeliverMajorInfo.ToJSON().Replace("\n", "").Replace("\r", "").Replace("@@", "");
                    //交付单状态选中
                    //DropDownListControl.DropDownListSelect(this.DliverState, instanceMajor[0].DliverState.ToString());
                }
                this.WorkTaskInsId.Value = this.GetStringFromQueryString("WorktaskInsId", "");
                this.WorkFlowInsId.Value = instanceMajor[0].WorkFlowInsId.ToString();
                wfid = instanceMajor[0].WorkFlowId.ToString();
                wtid = instanceMajor[0].WorkTaskId.ToString();
               
            }
        }

        #region-----------
        public string PageTitle
        { get { return this.GetStringFromQueryString("title", "") == "" ? "交付申请单--项目" : "交付申请单--项目"; } }
        public string strDescript
        { get { return this.GetStringFromQueryString("descript", ""); } }

        public string Flag
        { get { return this.GetStringFromQueryString("flag", ""); } }
        public string ListRecord
        { get { return GetListCrmProduct(); } }
        /// <summary>
        /// 信息集合
        /// </summary>
        public string ListDeliverMajorInfo
        { get { return this.strDeliverMajorInfo; } }
        private string GetListCrmProduct()
        {
            IList<V_CrmOrderProduct> list = order.GetAllOrderProduct(new Guid(this.ID.Value));
            if (list != null && list.Count > 0)
            {
                return ZX.Common.Globals.ObjectToJSON(list);
            }
            else
            {
                return "";
            }
        }
        public string strWorkFlowId
        { get { return this.GetStringFromQueryString("wfid", "") == "" ? wfid : this.GetStringFromQueryString("wfid", ""); } }
        /// <summary>
        /// 
        /// </summary>
        public string strWorkTaskId
        { get { return this.GetStringFromQueryString("wtid", "") == "" ? wtid : this.GetStringFromQueryString("wtid", ""); } }

        public string strOperatorInstanceId
        { get { return this.GetStringFromQueryString("OperatorInstanceId", ""); } }
        public string strWorkflowInsId
        { get { return this.GetStringFromQueryString("WorkflowInsId", ""); } }
        public string strView
        { get { return this.GetStringFromQueryString("view", ""); } }
        public string strBackPay { get { return unSatisy; } }
        public string strPM
        {
            get { return PM.Value; }
        }
        public string strStartDate
        {
            get { return txtStartDate.Value; }
        }
        public string strMemember
        {
            get { return Memember.Value; }
        }
        public string strExplain
        {
            get { return txtExplain.Value; }
        }
        public string strOperStatus
        {
            get { return this.GetStringFromQueryString("OperStatus", ""); }
        }
        public string strOrderProduct {
            get { return orderProduct; }
        }
        public string strEndDate
        {
            get { return DateTime.Now.ToString("yyyy-MM-dd"); }
        }
        /// <summary>
        /// 弹出列表客户经理ID
        /// </summary>
        public string addUser { get { return this.UserId.ToString(); } }
        public string strOrderId {
            get { return orderId; }
        }
        public string strProductId {
            get { return productId; }
        }
        #endregion
        /// <summary>
        /// 初始化流程数据
        /// </summary>
        /// <param name="workflowInsId"></param>
        public void Inits(string workflowInsId)
        {
            DataTable dt = client.GetWorkflowdetail(workflowInsId);
            //if (dt == null || dt.Rows.Count == 0)
            //{
            //    gvworkflowMessage.Visible = false;
            //}
            //else
            //{
            gvworkflowMessage.DataSource = dt;
            gvworkflowMessage.DataBind();
            //}
        }
    }
}